package com.xai.pg.util;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Kai
 * @Date 2016-1-14 10:53:20
 * @Desc
 */
public class Connection {

    java.sql.Connection conn = null;
    private String driver;
    private String url;
    private String user;
    private String password;
    private boolean status = true;

    public Connection(String driver, String url, String user, String password) {
        this.driver = driver;
        this.url = url;
        this.user = user;
        this.password = password;
        init();
    }

    public List<Object[]> select(String sql) {
        this.status = true;
        try {
            if (conn == null || conn.isClosed()) {
                init();
            }
            if (!conn.isClosed()) {
                ResultSet rs = null;
                List<Object[]> result = null;
                try {
                    Statement statement = conn.createStatement();
                    rs = statement.executeQuery(sql);
                    result = new ArrayList<Object[]>();
                    while (rs != null && rs.next()) {
                        ResultSetMetaData metaData = rs.getMetaData();
                        int columnCount = metaData.getColumnCount();
                        Object[] objs = new Object[columnCount];
                        for (int i = 0; i < columnCount; i++) {
                            objs[i] = rs.getObject(i + 1);
                        }
                        result.add(objs);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (rs != null) {
                    rs.close();
                }
                this.status = false;
                return result;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            close();
        } finally {
            this.status = false;
        }
        return null;
    }

    private void init() {
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            Logger.getLogger(Conf.class.getName()).log(Level.SEVERE, "数据库连接失败,请先修改conf.properties配置文件中数据库相关的配置");
            Logger.getLogger(Conf.class.getName()).log(Level.SEVERE, "The database connection failed. Please modify the database-related configuration in the conf.properties configuration file first.");
            e.printStackTrace();
            close();
        }
    }

    private void close() {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public boolean isStatus() {
        return status;
    }
}
